﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public class Deadlock
    {
        private object a;
        private object b;

        public void Start()
        {
            a = new object();
            b = new object();

            Thread thread1 = new Thread(start1);
            Thread thread2 = new Thread(start2);

            thread1.Start();
            thread2.Start();
        }

        private void start1()
        {
            lock (a)
            {
                MessageBox.Show("Thread1 - lock object a");

                lock (b)
                {
                    MessageBox.Show("Thread1 - lock object b");
                }
            }
        }

        private void start2()
        {
            lock (b)
            {
                MessageBox.Show("Thread2 - lock object b");

                lock (a)
                {
                    MessageBox.Show("Thread2 - lock object a");
                }
            }
        }
    }
}
